package cn.seapon.talker.db;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;
import cn.seapon.talker.bean.MessageEntity;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.GenericRawResults;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

/**
 * @author 作者 E-mail:
 * @version 创建时间：2015年1月9日 下午4:45:48 类说明
 */
public class MessageEntityDao {

	private static final String TAG = "||__MessageEntityDao__||";
	private Context context;
	private TalkerOrmliteHelper helper;
	private Dao<MessageEntity, Integer> messageEntityDao;

	public MessageEntityDao(Context context) {
		this.context = context;
		try {
			helper = TalkerOrmliteHelper.getHelper(context);
			messageEntityDao = helper.getMessageEntityDao();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加单条记录到数据库中
	 * 
	 * @param entity
	 */
	public void add(MessageEntity entity) {
		try {
			messageEntityDao.create(entity);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 添加一组记录到数据库中
	 * 
	 * @param list
	 */
	public void addAll(List<MessageEntity> list) {
		for (int i = 0; i < list.size(); i++) {
			add(list.get(i));
		}
	}

	public List<MessageEntity> getAll(MessageEntity entity) {
		try {
			return messageEntityDao.queryForMatching(entity);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 获得未读取得消息的数量
	 * 
	 * @return
	 */
	public int getUnreadNum(String account, String friendid) {
		try {
			GenericRawResults<String[]> rawResults =
					messageEntityDao.queryRaw(
							"select * from st_msg where account = '"+account+"'  and (sender = '"+friendid+"' or receiver = '"+friendid+"') and hasread = 0 ;");
					// there should be 1 result
					List<String[]> results = rawResults.getResults();
					return results.size();
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * @param account
	 * @param talker
	 * @param time
	 * @return int 成功返回删除的行号，失败返回-1
	 */
	public int delete(String account, String talker, long time) {
		try {
			DeleteBuilder<MessageEntity, Integer> builder = messageEntityDao
					.deleteBuilder();
			builder.where().eq("account", account).and().eq("sender", talker)
					.or().eq("receiver", talker).and().eq("posttime", time);
			return builder.delete();
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 清空历史
	 * 
	 * @param account
	 * @param talker
	 * @param time
	 * @return int 返回成功删除的数量，发生错误返回-1
	 */
	public int delete(String account, String talker) {
		try {
			DeleteBuilder<MessageEntity, Integer> builder = messageEntityDao
					.deleteBuilder();
			builder.where().eq("account", account).and().eq("sender", talker)
					.or().eq("receiver", talker);
			int ret = builder.delete();
			Log.d(TAG, "" + ret);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 标记该账户下的该联系人为全部已经读取
	 * 
	 * @param account
	 * @param friendid
	 * @return
	 */
	public int makeAllReaded(String account, String friendid) {
		try {
			UpdateBuilder<MessageEntity, Integer> builder = messageEntityDao
					.updateBuilder();
			builder.updateColumnValue("hasread", true);
			builder.where().eq("account", account).and().eq("sender", friendid)
					.or().eq("receiver", friendid);
			int ret = builder.update();
			Log.d(TAG, "   " + ret);
			return ret;
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}
	}

	public ArrayList<MessageEntity> getAllRecord(String account, String talker) {
		try {
			List<MessageEntity> list = messageEntityDao.queryBuilder().where()
					.eq("account", account).and().eq("sender", talker).or()
					.eq("receiver", talker).query();
			ArrayList<MessageEntity> entities = new ArrayList<MessageEntity>();
			for (int i = 0; i < list.size(); i++) {
				entities.add(list.get(i));
			}
			return entities;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

}
